home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter6 / setcolor.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  11KB  |  319 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "setcolor.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "App";
  20. LPCTSTR lpszTitle   = "ListView_SetBkColor()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. HWND hList = NULL;
  107.  
  108. LPCTSTR lpszData[4]  = { "Circle", "Rectangle", "Cross", "Check" };
  109. LPCTSTR lpszColor[4] = { "Yellow", "Red",       "Black", "Black" };
  110.  
  111. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  112. {
  113.  
  114.    switch( uMsg )
  115.    {
  116.       case WM_CREATE  :
  117.               {
  118.                  HIMAGELIST hImage, hSmall;
  119.  
  120.                  InitCommonControls();
  121.  
  122.                  // Create the image lists and the list view.
  123.                  //..........................................
  124.                  hImage = ImageList_Create( 32, 32, ILC_COLOR4 | ILC_MASK, 4, 1 ); 
  125.                  hSmall = ImageList_Create( 16, 16, ILC_COLOR4 | ILC_MASK, 4, 1 );
  126.                  hList  = CreateWindowEx( 0, WC_LISTVIEW, "",
  127.                                           WS_CHILD | WS_VISIBLE | LVS_REPORT | 
  128.                                           LVS_EDITLABELS | LVS_SINGLESEL,
  129.                                           0, 20, 100, 100,
  130.                                           hWnd,
  131.                                           (HMENU)1,
  132.                                           hInst,
  133.                                           NULL);
  134.  
  135.                  if ( hList && hImage && hSmall )
  136.                  {
  137.                     LV_COLUMN col;
  138.                     LV_ITEM   item;
  139.                     int       i;
  140.  
  141.                     // Set the large and small image lists.
  142.                     //.....................................
  143.                     ListView_SetImageList( hList, hImage, LVSIL_NORMAL );
  144.                     ListView_SetImageList( hList, hSmall, LVSIL_SMALL );
  145.  
  146.                     // Add the images to the image list.
  147.                     //..................................
  148.                     for( i=0; i<4; i++ )
  149.                     {
  150.                        ImageList_AddIcon( hImage, LoadIcon( hInst, lpszData[i] ) );
  151.                        ImageList_AddIcon( hSmall, LoadIcon( hInst, lpszData[i] ) );
  152.                     }
  153.  
  154.                     // Add the columns for the report view.
  155.                     //.....................................
  156.                     col.mask    = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; 
  157.                     col.fmt     = LVCFMT_LEFT; 
  158.                     col.cx      = 100; 
  159.  
  160.                     col.pszText  = "Name"; 
  161.                     col.iSubItem = 0;
  162.                     ListView_InsertColumn( hList, 0, &col );
  163.                     
  164.                     col.pszText  = "Color"; 
  165.                     col.iSubItem = 1;
  166.                     ListView_InsertColumn( hList, 1, &col );
  167.  
  168.                     // Insert items into the list view.
  169.                     //.................................
  170.                     for( i=0; i<4; i++ )
  171.                     {
  172.                        item.mask     = LVIF_TEXT | LVIF_IMAGE;
  173.                        item.pszText  = (LPTSTR)lpszData[i];
  174.                        item.iItem    = i;
  175.                        item.iSubItem = 0;
  176.                        item.iImage   = i;
  177.                        ListView_InsertItem( hList, &item );
  178.  
  179.                        item.mask     = LVIF_TEXT;
  180.                        item.pszText  = (LPTSTR)lpszColor[i];
  181.                        item.iSubItem = 1;
  182.                        ListView_SetItem( hList, &item );
  183.                     }
  184.                  }
  185.               }
  186.               break;
  187.  
  188.       case WM_SIZE :
  189.               if ( hList )
  190.                  MoveWindow( hList, 0, 0, LOWORD( lParam ), HIWORD( lParam ), FALSE );
  191.  
  192.               if ( wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED )
  193.                  ListView_RedrawItems( hList, 0, ListView_GetItemCount( hList ) );
  194.               break;
  195.  
  196.       case WM_COMMAND :
  197.               switch( LOWORD( wParam ) )
  198.               {
  199.                  case IDM_OPTIONS :
  200.                         DialogBox( hInst, "OptionsDialog", hWnd, (DLGPROC)OptionsDlg );
  201.                         break;      
  202.  
  203.                  case IDM_ABOUT :
  204.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  205.                         break;
  206.  
  207.                  case IDM_EXIT :
  208.                         DestroyWindow( hWnd );
  209.                         break;
  210.               }
  211.               break;
  212.       
  213.       case WM_DESTROY :
  214.               if ( ListView_GetImageList( hList, LVSIL_NORMAL ) )
  215.                  ImageList_Destroy( ListView_GetImageList( hList, LVSIL_NORMAL ) );
  216.  
  217.               if ( ListView_GetImageList( hList, LVSIL_SMALL ) )
  218.                  ImageList_Destroy( ListView_GetImageList( hList, LVSIL_SMALL ) );
  219.  
  220.               PostQuitMessage(0);
  221.               break;
  222.  
  223.       default :
  224.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  225.    }
  226.  
  227.    return( 0L );
  228. }
  229.  
  230.  
  231. LRESULT CALLBACK OptionsDlg( HWND hDlg,           
  232.                              UINT message,        
  233.                              WPARAM wParam,       
  234.                              LPARAM lParam)
  235. {
  236.    switch (message) 
  237.    {
  238.        case WM_INITDIALOG: 
  239.                {
  240.                   COLORREF cr;
  241.  
  242.                   // Retrieve the list view colors.
  243.                   //...............................
  244.                   cr = ListView_GetTextColor( hList );
  245.                   SetDlgItemInt( hDlg, IDC_TR, GetRValue( cr ), FALSE );
  246.                   SetDlgItemInt( hDlg, IDC_TG, GetGValue( cr ), FALSE );
  247.                   SetDlgItemInt( hDlg, IDC_TB, GetBValue( cr ), FALSE );
  248.  
  249.                   cr = ListView_GetTextBkColor( hList );
  250.                   SetDlgItemInt( hDlg, IDC_TBR, GetRValue( cr ), FALSE );
  251.                   SetDlgItemInt( hDlg, IDC_TBG, GetGValue( cr ), FALSE );
  252.                   SetDlgItemInt( hDlg, IDC_TBB, GetBValue( cr ), FALSE );
  253.  
  254.                   cr = ListView_GetBkColor( hList );
  255.                   SetDlgItemInt( hDlg, IDC_BR, GetRValue( cr ), FALSE );
  256.                   SetDlgItemInt( hDlg, IDC_BG, GetGValue( cr ), FALSE );
  257.                   SetDlgItemInt( hDlg, IDC_BB, GetBValue( cr ), FALSE );
  258.                }
  259.                return (TRUE);
  260.  
  261.        case WM_COMMAND:                              
  262.                if ( LOWORD(wParam) == IDOK )       
  263.                {
  264.                   // Update the list view colors.
  265.                   //............................. 
  266.                   ListView_SetTextColor( hList, 
  267.                            RGB( GetDlgItemInt( hDlg, IDC_TR, NULL, FALSE ),
  268.                                 GetDlgItemInt( hDlg, IDC_TG, NULL, FALSE ),
  269.                                 GetDlgItemInt( hDlg, IDC_TB, NULL, FALSE ) ) );
  270.  
  271.                   ListView_SetTextBkColor( hList, 
  272.                            RGB( GetDlgItemInt( hDlg, IDC_TBR, NULL, FALSE ),
  273.                                 GetDlgItemInt( hDlg, IDC_TBG, NULL, FALSE ),
  274.                                 GetDlgItemInt( hDlg, IDC_TBB, NULL, FALSE ) ) );
  275.  
  276.                   ListView_SetBkColor( hList, 
  277.                            RGB( GetDlgItemInt( hDlg, IDC_BR, NULL, FALSE ),
  278.                                 GetDlgItemInt( hDlg, IDC_BG, NULL, FALSE ),
  279.                                 GetDlgItemInt( hDlg, IDC_BB, NULL, FALSE ) ) );
  280.  
  281.                   ListView_RedrawItems( hList, 0, ListView_GetItemCount( hList ) );
  282.  
  283.                   EndDialog( hDlg, IDOK );
  284.                }
  285.                else if ( LOWORD(wParam) == IDCANCEL )    
  286.                {
  287.                   EndDialog(hDlg, IDCANCEL);        
  288.                   return (TRUE);
  289.                }
  290.                break;
  291.    }
  292.  
  293.    return (FALSE); 
  294. }
  295.  
  296.  
  297. LRESULT CALLBACK About( HWND hDlg,           
  298.                         UINT message,        
  299.                         WPARAM wParam,       
  300.                         LPARAM lParam)
  301. {
  302.    switch (message) 
  303.    {
  304.        case WM_INITDIALOG: 
  305.                return (TRUE);
  306.  
  307.        case WM_COMMAND:                              
  308.                if (   LOWORD(wParam) == IDOK         
  309.                    || LOWORD(wParam) == IDCANCEL)    
  310.                {
  311.                        EndDialog(hDlg, TRUE);        
  312.                        return (TRUE);
  313.                }
  314.                break;
  315.    }
  316.  
  317.    return (FALSE); 
  318. }
  319.